package com.zz.framework.core.ext

import android.widget.EditText

/**
 * Copyright © 2022 StariverPool All rights reserved.
 * Created on 2022/3/16 4:18 下午.
 * @version: V1.0
 */

fun EditText.editable(editale:Boolean){
    this.isFocusable = editale;
    this.isCursorVisible = editale;
    this.isFocusableInTouchMode = editale;
    if (editale){
        this.requestFocus();
    }
}

//launch(UI) {
//    editText.onTextChanged()
//        .debounce(1, TimeUnit.SECONDS)
//        .consumeEach {
//            Log.d("DebounceTest", "value: $it")
//        }
//}
//}
//fun EditText.onTextChanged(): ReceiveChannel<String> =
//    Channel<String>(capacity = Channel.UNLIMITED).also { channel ->
//        addTextChangedListener(object : TextWatcher {
//            override fun afterTextChanged(editable: Editable?) {
//                editable?.toString().orEmpty().let(channel::offer)
//            }
//
//            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
//            }
//
//            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
//            }
//
//        })
//    }
//
//fun <T> ReceiveChannel<T>.debounce(time: Long, unit: TimeUnit = TimeUnit.MILLISECONDS): ReceiveChannel<T> =
//    Channel<T>(capacity = Channel.CONFLATED).also { channel ->
//        launch {
//            var value = receive()
//            whileSelect {
//                onTimeout(time, unit) {
//                    channel.offer(value)
//                    value = receive()
//                    true
//                }
//                onReceive {
//                    value = it
//                    true
//                }
//            }
//        }
//    }